﻿<%@ WebHandler Language="C#" Class="CalendarHandler" %>

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using EVideoDataService;
using VideoClassLibrary.BusinessLogicLayer;
using VideoClassLibrary.CacheManager;
using VideoClassLibrary.Extension;
using VideoClassLibrary.Model;

public class CalendarHandler : IHttpHandler, IReadOnlySessionState
{

    AjaxUtilJson rtn = new AjaxUtilJson();
    public void ProcessRequest (HttpContext context) {
        int currentY = 0;
        int currentM = 0;
        int currentMd = 0;

        var courseWeek = new CourseWeek();
        DateTime minTime = new DateTime();
        DateTime maxTime = new DateTime();
        DataSet dataSet=  CommonCacheManager.GetCacheData<DataSet>("CourseList");
        V_adminUser vAdminUser = (V_adminUser)context.Session["currentAdminUser"];
        string opType = context.Request.Form["OpType"];
        switch (opType)
        {

            case "CalendarCourseList":
                GetCourseList(context, vAdminUser);
                break;
            case "PreWeek":
                currentY = int.Parse(context.Request["CurrentY"]);
                currentM = int.Parse(context.Request["CurrentM"]);
                currentMd = int.Parse(context.Request["CurrentMd"]);
                rtn.ResultMessage = courseWeek.GetCourseWeek(currentY, currentM, currentMd, 1, ref minTime, ref maxTime, dataSet);
                rtn.RtnMinYear = minTime.Year;
                rtn.RtnMinMonth = minTime.Month;
                rtn.RtnMinDay = minTime.Day;
                rtn.RtnMaxYear = maxTime.Year;
                rtn.RtnMaxMonth = maxTime.Month;
                rtn.RtnMaxDay = maxTime.Day;
                break;
            case "NextWeek":
                currentY = int.Parse(context.Request["CurrentY"]);
                currentM = int.Parse(context.Request["CurrentM"]);
                currentMd = int.Parse(context.Request["CurrentMd"]);
                rtn.ResultMessage = courseWeek.GetCourseWeek(currentY, currentM, currentMd, 2, ref  minTime, ref  maxTime, dataSet);
                rtn.RtnMinYear = minTime.Year;
                rtn.RtnMinMonth = minTime.Month;
                rtn.RtnMinDay = minTime.Day;
                rtn.RtnMaxYear = maxTime.Year;
                rtn.RtnMaxMonth = maxTime.Month;
                rtn.RtnMaxDay = maxTime.Day;
                break;
        }




        context.Response.ContentType = "text/json;charset=UTF-8;";
        context.Response.Write(rtn.ToJsonItem());
    }
    private void GetCourseList(HttpContext context,V_adminUser vAdminUser)
    {
        DateTime courseDate = DateTime.Parse(context.Request["CourseDate"]);
        DateTime startTime = DateTime.Parse(context.Request["StartTime"]);
        DateTime endTime = DateTime.Parse(context.Request["EndTime"]);
        int roomId = int.Parse(context.Request["RoomId"]);
        DateTime start = new DateTime(courseDate.Year, courseDate.Month, courseDate.Day, startTime.Hour, startTime.Minute, startTime.Second);
        DateTime end = new DateTime(courseDate.Year, courseDate.Month, courseDate.Day, endTime.Hour, endTime.Minute, endTime.Second);


        #region 绑定视频的相关信息
        int courseListId = int.Parse(context.Request["CourseID"]);
        CourseListModule courseHelper = new CourseListModule();
        DataSet ds = courseHelper.GetCourseDetailByDestId(vAdminUser.SchoolID, courseListId);
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            int cj = Convert.ToInt32(context.Request["CurrentIndex"]) + 1;
            DataRow dr = ds.Tables[0].Rows[0];
            rtn.TeacherName = dr["vt_realname"].ToString();
            rtn.CourseName = dr["vc_name"].ToString() + "&nbsp;第" + cj + "节";
            rtn.CourseDesc = dr["vc_desc"].ToString();

        }
        #endregion
        

        VideoInfoService vi = new VideoInfoService();
        List<VFInfo> list = new List<VFInfo>();
        list = vi.GetVSInfoListByTimespan(vAdminUser.SchoolID, roomId, start, end, VFType.SD);
        //List<VFInfo> list2= new List<VFInfo>();
        // IEnumerable<VFInfo>  list2= list.Distinct < List<VFInfo>>(list).ToList();


        ArrayList tempAL = new ArrayList();

        List<VFInfo> tempList = new List<VFInfo>();
        foreach (var vfInfo in list)
        {
            StringBuilder tempSB = new StringBuilder();
            tempSB.Append(vfInfo.VFUrl);
            tempSB.Append("\\");
            tempSB.Append(vfInfo.VFName);

            if (!tempAL.Contains(tempSB.ToString()))
            {
                tempAL.Add((tempSB.ToString()));
                tempList.Add(vfInfo);
            }

        }

        list = tempList.OrderBy(s => s.VSTime).ToList();
        //通过webservice返回的视频地址列表 放入缓存
        CommonCacheManager.AddCacheData<List<VFInfo>>("VideoPlayUrlList" + 1, list, 60);
        string js = string.Empty;
        StringBuilder sbHtml = new StringBuilder();
        string turl = context.Request.Url.ToString();
        if (turl.IndexOf("?showflvid=") > 0)
            turl = turl.Remove(context.Request.Url.ToString().IndexOf("?showflvid="));

        if (list.Count > 0)
        {
            sbHtml.AppendLine("<div class=\"playTable\">");
            for (int i = 0; i < list.Count; i++)
            {
                if (!string.IsNullOrEmpty(context.Request["CurrentIndex"]))
                {

                    js = SetWhichVideoPlaying(list, int.Parse(context.Request["CurrentIndex"]));
                    ClassRoomModule classRoomModule = new ClassRoomModule();

                    if (classRoomModule.ChkExsit(Convert.ToInt32(list[int.Parse(context.Request["CurrentIndex"])].VSID)) > 0)
                    {
                        js = "";

                    }
                    rtn.ReturnValue = js;

                }
                else
                {
                    js = SetWhichVideoPlaying(list, 0);
                    rtn.ReturnValue = js;


                }

                if (i % 8 == 0)
                {
                    sbHtml.AppendLine("<ul>");
                }
                if (Convert.ToInt32(context.Request["CurrentIndex"]) == i)
                {
                    sbHtml.AppendLine("<li> <a href=\"javascript:VideoPalyByIndex('" + i + "')\"><font color=red><strong>第" + (i + 1) +
                                      "段</strong></font></a></li>");
                }
                else
                {
                    sbHtml.AppendLine("<li> <a href=\"javascript:VideoPalyByIndex('" + i + "')\">第" + (i + 1) +
                                          "段</a></li>");
                }
                if ((i + 1) % 8 == 0)
                {
                    sbHtml.AppendLine("</ul>");
                }


            }
            sbHtml.AppendLine("</div >");
         

        }




        rtn.ResultMessage = sbHtml.ToString();


    }

    private string SetWhichVideoPlaying(List<VFInfo> list, int index)
    {
        StringBuilder sbHtml = new StringBuilder();
        string flvPath = list[index].VFUrl;
        string ImgPath = list[index].vpInfo.VPUrl;
        string vSID = list[index].VSID.ToString();
    
        sbHtml.AppendLine("<script type='text/javascript'>    LoadPlayer('" + flvPath + "','" + ImgPath + "','" + vSID + "');</script>");
        return sbHtml.ToString();
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}